package com.ec.rs.aop;

import com.ec.common.constant.CacheConstants;
import com.ec.common.utils.CacheClient;
import com.ec.rs.domain.po.Bed;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class CacheEvictionAspect {
    private final CacheClient cacheClient;

    @AfterReturning("execution(* com.ec.rs.mapper.BedMapper.updateById(..)) || execution(* com.ec.rs.mapper.BedMapper.deleteById(..))")
    public void evictBedCache(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Integer id = null;
        if (args[0] instanceof Bed) {
            id = ((Bed) args[0]).getId();
        } else if (args[0] instanceof Integer) {
            id = (Integer) args[0];
        }
        if (id != null) {
            // 删除单条床位缓存
            cacheClient.deleteCache(CacheConstants.CACHE_BED_KEY + id);
            log.info("床位缓存删除成功, id: {}", id);
        }
    }
}